package baseFx.common.utils;


public class NumberUtils {
    private static final String baseDigits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final int BASE = baseDigits.length();
    private static final char[] digitsChar = baseDigits.toCharArray();
    private static final int FAST_SIZE = 'z';
    private static final int[] digitsIndex = new int[FAST_SIZE + 1];

    static {
        for (int i = 0; i < FAST_SIZE; i++) {
            digitsIndex[i] = -1;
        }
        for (int i = 0; i < BASE; i++) {
            digitsIndex[digitsChar[i]] = i;
        }
    }

    public static String to64(long value) {
        if (value == 0) {
            return "0";
        }
        StringBuilder buf = new StringBuilder();
        while (value != 0) {
            buf.append(digitsChar[(int) (value % BASE)]);
            value /= BASE;
        }
        return buf.reverse().toString();
    }

    public static long numBy64(String value) {
        long result = 0L;
        long multiplier = 1;
        for (int pos = value.length() - 1; pos >= 0; pos--) {
            int index = digitsIndex[value.charAt(pos)];
            result += index * multiplier;
            multiplier *= BASE;
        }
        return result;
    }

    public static String to32(long value) {
        return Long.toString(value, 32);
    }

    public static long numBy32(String value) {
        return Long.parseLong(value, 32);
    }

//    public static void main(String[] args) {
//        String str = to64(352604749499077155L);
//        System.out.println(str);
//        System.out.println(numBy64(str));
//    }
}
